package com.ruoyi.front.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.BusinessClock;
import com.ruoyi.business.mapper.BusinessClockMapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.front.service.IFrontClockService;

/**
 * 考勤Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-04
 */
@Service
public class FrontClockServiceImpl implements IFrontClockService
{
    @Autowired
    private BusinessClockMapper businessClockMapper;

    /**
     * 查询考勤
     * 
     * @param clockId 考勤主键
     * @return 考勤
     */
    @Override
    public BusinessClock selectBusinessClockByClockId(Long clockId)
    {
        return businessClockMapper.selectBusinessClockByClockId(clockId);
    }

    /**
     * 查询考勤列表
     * 
     * @param businessClock 考勤
     * @return 考勤
     */
    @Override
    public List<BusinessClock> selectBusinessClockList(BusinessClock businessClock)
    {
        businessClock.setUserName(SecurityUtils.getLoginUser().getUser().getUserName());
        return businessClockMapper.selectBusinessClockList(businessClock);
    }

    /**
     * 新增考勤
     * 
     * @param businessClock 考勤
     * @return 结果
     */
    @Override
    public int insertBusinessClock(BusinessClock businessClock)
    {
        businessClock.setUserName(SecurityUtils.getLoginUser().getUser().getUserName());
        businessClock.setUserNick(SecurityUtils.getLoginUser().getUser().getNickName());
        Date clockTime=new Date();
        businessClock.setClockTime(clockTime);
// 获取今日的 8:00、12:30、14:00 和 17:00 的时间点
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(clockTime);

// 设置为今日的 8:00
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date eightAm = calendar.getTime();

// 设置为今日的 12:30
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 30);
        Date twelveThirtyPm = calendar.getTime();

// 设置为今日的 14:00
        calendar.set(Calendar.HOUR_OF_DAY, 14);
        calendar.set(Calendar.MINUTE, 0);
        Date twoPm = calendar.getTime();

// 设置为今日的 17:00
        calendar.set(Calendar.HOUR_OF_DAY, 17);
        calendar.set(Calendar.MINUTE, 0);
        Date fivePm = calendar.getTime();

// 如果时间小于今日 8:00
        if (clockTime.before(eightAm)) {
            businessClock.setClockStatus(1L);
        }
// 如果时间大于今日 17:00
        if (clockTime.after(fivePm)) {
            businessClock.setClockStatus(2L);
        }
// 如果时间大于今日 8:00 且小于 12:30
        if (clockTime.after(eightAm) && clockTime.before(twelveThirtyPm)) {
            businessClock.setClockStatus(3L);
        }
// 如果时间大于今日 14:00 且小于 17:00
        if (clockTime.after(twoPm) && clockTime.before(fivePm)) {
            businessClock.setClockStatus(4L);
        }
        return businessClockMapper.insertBusinessClock(businessClock);
    }

    /**
     * 修改考勤
     * 
     * @param businessClock 考勤
     * @return 结果
     */
    @Override
    public int updateBusinessClock(BusinessClock businessClock)
    {
        return businessClockMapper.updateBusinessClock(businessClock);
    }

    /**
     * 批量删除考勤
     * 
     * @param clockIds 需要删除的考勤主键
     * @return 结果
     */
    @Override
    public int deleteBusinessClockByClockIds(Long[] clockIds)
    {
        return businessClockMapper.deleteBusinessClockByClockIds(clockIds);
    }

    /**
     * 删除考勤信息
     * 
     * @param clockId 考勤主键
     * @return 结果
     */
    @Override
    public int deleteBusinessClockByClockId(Long clockId)
    {
        return businessClockMapper.deleteBusinessClockByClockId(clockId);
    }
}
